package top.idcard.agent.biz.util;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * @author wx
 * @version 1.0
 * @date 2021/2/18 3:21 下午
 */
public class ByteUtil {

    public static byte[] string2Byte(String str){
        return str.getBytes();
    }

    public static byte[] string2Byte(String str, Integer len){
        byte[] bytes = new byte[len];
        byte[] temp = str.getBytes();
        System.arraycopy(temp, 0, bytes, 0, temp.length);
        return bytes;
    }

    public static byte[] short2Byte(Short value){
        return new byte[]{value.byteValue()};
    }

    public static byte[] short2Byte(Short value, Integer len){
        return ByteBuffer.allocate(len).order(ByteOrder.BIG_ENDIAN).putShort(value).array();
    }

    public static byte[] img2Byte(byte[] imgBytes){
        byte[] bytes = new byte[38862];
        System.arraycopy(imgBytes, 0, bytes, 0, imgBytes.length);
        return bytes;
    }

    public static byte[] Fingerprint2Byte(byte[] imgBytes){
        byte[] bytes = new byte[1024];
        System.arraycopy(imgBytes, 0, bytes, 0, imgBytes.length);
        return bytes;
    }

    private static ByteOrder endian = ByteOrder.LITTLE_ENDIAN;

    public static Boolean bytesToBool(byte[] bytes) {
        return bytes[0] == '1';
    }

    public static byte[] byteToBytes(byte b) {
        return ByteBuffer.allocate(1).order(endian).put(b).array();
    }

    public static char bytesToChar(byte[] b, int offset) {
        return (char) b[offset];
    }

    public static short byteToShort(byte[] b) {
        return ByteBuffer.wrap(b).order(endian).getShort();
    }

    public static short byteToShort(byte[] b, int offset) {
        return ByteBuffer.wrap(b, offset, 2).order(endian).getShort();
    }

    public static int byteToInt(byte[] src) {
        return byteToInt(src, 0);
    }

    public static int byteToInt(byte[] src, int offset) {
        return ByteBuffer.wrap(src, offset, 4).order(endian).getInt();
    }

    public static long bytesToLong(byte[] b) {
        return bytesToLong(b, 0);
    }

    public static long bytesToLong(byte[] b, int offset) {
        return ByteBuffer.wrap(b, offset, 8).order(endian).getLong();
    }

    public static String byteToString(byte[] b) {
        return new String(b);
    }

    public static String byteToString(byte[] b, int offset, int length) {
        return new String(b, offset, length);
    }

    public static String byteToString(byte[] b, int offset, int length, String charset) {
        try {
            return new String(b, offset, length, charset);
        } catch (Exception e) {
            return new String(b, offset, length);
        }
    }

    public static byte[] boolToBytes(Boolean o) {
        byte[] bytes = new byte[1];
        if (o) {
            bytes[0] = '1';
        } else {
            bytes[0] = '0';
        }
        return bytes;
    }

    public static byte[] charToBytes(char side) {
        return new byte[] {(byte) side};
    }

    public static byte[] shortToBytes(short nubmer) {
        return ByteBuffer.allocate(2).order(endian).putShort(nubmer).array();
    }

    public static byte[] intToBytes(int i) {
        return ByteBuffer.allocate(4).order(endian).putInt(i).array();
    }

    public static byte[] longToBytes(long number) {
        return ByteBuffer.allocate(8).order(endian).putLong(number).array();
    }

    public static String printBytes(byte[] bytes) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            stringBuilder.append(String.format("%02X%s", bytes[i], (i < bytes.length - 1) ? "-" : ""));
        }

        return stringBuilder.toString();
    }

    public static byte[] subByteArray(byte[] bytes, int offset, int length){
        byte[] data = new byte[length];
        System.arraycopy(bytes, offset, data, 0, length);
        return data;
    }
}
